//
//  ZYCStuLeaveRecordDetailFooterView.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/27.
//

#import "ZYCStuLeaveRecordDetailFooterView.h"
#import "TZTestCell.h"
#import "ZYCLeaveRecordDetailModel.h"
#import "ZYCCollectionImageCell.h"


@interface ZYCStuLeaveRecordDetailFooterView ()<UICollectionViewDataSource,UICollectionViewDelegate,YBImageBrowserDataSource>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSMutableArray *imageArray;


@end
@implementation ZYCStuLeaveRecordDetailFooterView



- (void)setModel:(ZYCLeaveRecordDetailModel *)model{
    _model = model;
    _contentLabel.text = model.leaveContent;
    self.dataSource = model.files;
    [self.collectionView reloadData];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZYCCollectionImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCCollectionImageCell class]) forIndexPath:indexPath];
    cell.fileModel = self.dataSource[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *browserDataArr = [NSMutableArray array];
    NSArray <ZYCLeaveFileModel *>*filesArray = self.model.files;
    NSUInteger index = indexPath.row;
    [filesArray enumerateObjectsUsingBlock:^(ZYCLeaveFileModel * _Nonnull fileModel, NSUInteger idx, BOOL * _Nonnull stop) {
        YBIBImageData *data = [YBIBImageData new];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/zd-system-service%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],fileModel.filePath]];
        data.imageURL = url;
        [browserDataArr addObject:data];
    }];

    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = browserDataArr;
    browser.currentPage = index;
    [browser show];
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEX_RGB(kBgWhiteColor);
        [self addSubview:self.titleLabel];
        [self addSubview:self.bgView];
        [self addSubview:self.contentLabel];
        [self addSubview:self.line];
        [self addSubview:self.collectionView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.height.mas_equalTo(10.0f);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(14.0f);
            make.top.equalTo(self.bgView.mas_bottom).offset(15.0f);
        }];

        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15.0f);
            make.height.mas_equalTo(1.0f);
        }];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(14.0f);
            make.right.equalTo(self).offset(-14.0f);
            make.top.equalTo(self.line.mas_bottom).offset(16.0f);
        }];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(14.0f);
            make.right.equalTo(self).offset(-14.0f);
            make.bottom.equalTo(self).offset(-14.0f);
            make.top.equalTo(self.contentLabel.mas_bottom).offset(16.0f);
        }];
    }
    return self;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel =   [UILabel LabelWithString:@"说明" Font:kCustomFont(@"PingFangSC-Light", 16.0f) titleColor:HEX_RGB(@"#999999") textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}
- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 16.0f) titleColor:HEX_RGB(@"#999999") textAlignment:ZZTextAlignmentLeft];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

- (UILabel *)line {
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EBEBEB");
    }
    return _line;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = HEX_RGB(@"#F9F9F9");
    }
    return _bgView;
}
-(UICollectionView *) collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZYCCollectionImageCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([ZYCCollectionImageCell class])];
    }
    return _collectionView;
}

-(UICollectionViewFlowLayout *) layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.itemSize = CGSizeMake(98.0, 98.0f);
        _layout.minimumLineSpacing = 0;//行间距
        _layout.minimumInteritemSpacing = 0;//列间距
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _layout;
}

- (NSMutableArray *)imageArray {
    if (!_imageArray) {
        _imageArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _imageArray;
}

- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSArray alloc] init];
    }
    return _dataSource;
}
@end
